{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 示例代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.15407373868365992\n",
      "0.33814496857384646\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "from sklearn.linear_model import Ridge\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "\n",
    "train_size = 20\n",
    "test_size = 12\n",
    "train_X = np.random.uniform(low=0, high=1.2, size=train_size)\n",
    "test_X = np.random.uniform(low=0.1, high=1.3, size=test_size)\n",
    "train_y = np.sin(train_X * 2 * np.pi) + np.random.normal(0, 0.2, train_size)\n",
    "test_y = np.sin(test_X * 2 * np.pi) + np.random.normal(0, 0.2, test_size)\n",
    "poly = PolynomialFeatures(6) # 次数为6\n",
    "train_poly_X = poly.fit_transform(train_X.reshape(train_size, 1))\n",
    "test_poly_X = poly.fit_transform(test_X.reshape(test_size, 1))\n",
    "model = Ridge(alpha=1.0)\n",
    "model.fit(train_poly_X, train_y)\n",
    "train_pred_y = model.predict(train_poly_X)\n",
    "test_pred_y = model.predict(test_poly_X)\n",
    "print(mean_squared_error(train_pred_y, train_y))\n",
    "print(mean_squared_error(test_pred_y, test_y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
